﻿using System;
using fastCSharp;
using fastCSharp.code.cSharp;

namespace diantou.web
{
    /// <summary>
    /// 通知
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPage = false, IsPool = true)]
    partial class notice : currentUserView<notice>
    {
        /// <summary>
        /// 通知项
        /// </summary>
        public struct item
        {
            /// <summary>
            /// 通知
            /// </summary>
            public diantou.dataAccess.notice Notice;
            /// <summary>
            /// 用户
            /// </summary>
            private diantou.dataAccess.user user;
            /// <summary>
            /// 用户
            /// </summary>
            public diantou.dataAccess.user User
            {
                get
                {
                    if (user == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.User) != 0)
                    {
                        if (Notice.QuestionId != 0 && !diantou.dataAccess.notice.NoAnonymousTypeMap.Get((int)Notice.Type))
                        {
                            user = diantou.dataAccess.clientCache.User[diantou.dataAccess.tcpCall.focusQuestion.anonymousUserId(Notice.UserId, Notice.QuestionId)];
                        }
                        else
                        {
                            user = diantou.dataAccess.clientCache.User[Notice.UserId];
                        }
                        if (user == null) user = diantou.dataAccess.user.NullUser;
                    }
                    return user != diantou.dataAccess.user.NullUser ? user : null;
                }
            }
            /// <summary>
            /// 话题
            /// </summary>
            private diantou.dataAccess.topic topic;
            /// <summary>
            /// 话题
            /// </summary>
            public diantou.dataAccess.topic Topic
            {
                get
                {
                    if (topic == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Topic) != 0)
                    {
                        topic = diantou.dataAccess.tcpCall.topic.Get(Notice.TopicId);
                        if (topic == null) topic = diantou.dataAccess.topic.NullTopic;
                    }
                    return topic != diantou.dataAccess.topic.NullTopic ? topic : null;
                }
            }
            /// <summary>
            /// 问题
            /// </summary>
            private diantou.dataAccess.question question;
            /// <summary>
            /// 问题
            /// </summary>
            public diantou.dataAccess.question Question
            {
                get
                {
                    if (question == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Question) != 0)
                    {
                        question = diantou.dataAccess.clientCache.Question[Notice.QuestionId];
                        if (question == null) question = diantou.dataAccess.question.NullQuestion;
                    }
                    return question != diantou.dataAccess.question.NullQuestion ? question : null;
                }
            }
            /// <summary>
            /// 回答
            /// </summary>
            private diantou.dataAccess.answer answer;
            /// <summary>
            /// 回答
            /// </summary>
            public diantou.dataAccess.answer Answer
            {
                get
                {
                    if (answer == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Answer) != 0)
                    {
                        answer = diantou.dataAccess.tcpCall.answer.Get(Notice.AnswerId);
                        if (answer == null) answer = diantou.dataAccess.answer.NullAnswer;
                    }
                    return answer != diantou.dataAccess.answer.NullAnswer ? answer : null;
                }
            }
            /// <summary>
            /// 收藏
            /// </summary>
            private diantou.dataAccess.favorite favorite;
            /// <summary>
            /// 收藏
            /// </summary>
            public diantou.dataAccess.favorite Favorite
            {
                get
                {
                    if (favorite == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Favorite) != 0)
                    {
                        favorite = diantou.dataAccess.clientCache.Favorite[Notice.FavoriteId];
                        if (favorite == null) favorite = diantou.dataAccess.favorite.NullFavorite;
                    }
                    return favorite != diantou.dataAccess.favorite.NullFavorite ? favorite : null;
                }
            }
            /// <summary>
            /// 勋章
            /// </summary>
            private diantou.dataAccess.medal medal;
            /// <summary>
            /// 勋章
            /// </summary>
            public diantou.dataAccess.medal Medal
            {
                get
                {
                    if (medal == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Medal) != 0)
                    {
                        medal = diantou.dataAccess.clientCache.Medal[Notice.MedalId];
                        if (medal == null) medal = diantou.dataAccess.medal.NullMedal;
                    }
                    return medal != diantou.dataAccess.medal.NullMedal ? medal : null;
                }
            }
            /// <summary>
            /// OJ问题
            /// </summary>
            private diantou.dataAccess.problem problem;
            /// <summary>
            /// OJ问题
            /// </summary>
            public diantou.dataAccess.problem Problem
            {
                get
                {
                    if (problem == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Problem) != 0)
                    {
                        problem = diantou.dataAccess.clientCache.Problem[Notice.ProblemId];
                        if (problem == null) problem = diantou.dataAccess.problem.NullProblem;
                    }
                    return problem != diantou.dataAccess.problem.NullProblem ? problem : null;
                }
            }
            /// <summary>
            /// 比赛
            /// </summary>
            private diantou.dataAccess.contest contest;
            /// <summary>
            /// 比赛
            /// </summary>
            public diantou.dataAccess.contest Contest
            {
                get
                {
                    if (contest == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Contest) != 0)
                    {
                        contest = diantou.dataAccess.clientCache.Contest[Notice.ContestId];
                        if (contest == null) contest = diantou.dataAccess.contest.NullContest;
                    }
                    return contest != diantou.dataAccess.contest.NullContest ? contest : null;
                }
            }
            /// <summary>
            /// 教程
            /// </summary>
            public diantou.dataAccess.tutorial tutorial;
            /// <summary>
            /// 教程
            /// </summary>
            public diantou.dataAccess.tutorial Tutorial
            {
                get
                {
                    if (tutorial == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.Tutorial) != 0)
                    {
                        tutorial = diantou.dataAccess.clientCache.Tutorial[Notice.TutorialId];
                        if (tutorial == null) tutorial = diantou.dataAccess.tutorial.NullTutorial;
                    }
                    return tutorial != diantou.dataAccess.tutorial.NullTutorial ? tutorial : null;
                }
            }
            ///// <summary>
            ///// 问题评论
            ///// </summary>
            //private diantou.dataAccess.questionComment questionComment;
            ///// <summary>
            ///// 问题评论
            ///// </summary>
            //public diantou.dataAccess.questionComment QuestionComment
            //{
            //    get
            //    {
            //        if (questionComment == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.QuestionComment) != 0)
            //        {
            //            questionComment = diantou.dataAccess.tcpCall.questionComment.Get(Notice.CommentId);
            //            if (questionComment == null) questionComment = diantou.dataAccess.questionComment.NullComment;
            //        }
            //        return questionComment != diantou.dataAccess.questionComment.NullComment ? questionComment : null;
            //    }
            //}
            ///// <summary>
            ///// 回答评论
            ///// </summary>
            //private diantou.dataAccess.answerComment answerComment;
            ///// <summary>
            ///// 回答评论
            ///// </summary>
            //public diantou.dataAccess.answerComment AnswerComment
            //{
            //    get
            //    {
            //        if (answerComment == null && (fastCSharp.Enum<diantou.dataAccess.notice.type, diantou.dataAccess.notice.noticeInfo>.Array(Notice.Type).IsValue & diantou.dataModel.isValue.AnswerComment) != 0)
            //        {
            //            answerComment = diantou.dataAccess.tcpCall.answerComment.Get(Notice.CommentId);
            //            if (answerComment == null) answerComment = diantou.dataAccess.answerComment.NullComment;
            //        }
            //        return answerComment != diantou.dataAccess.answerComment.NullComment ? answerComment : null;
            //    }
            //}


            /// <summary>
            /// 目标URL
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public string Path
            {
                get
                {
                    switch (Notice.Type)
                    {
                        case diantou.dataModel.notice.type.UserAgreeAnswer:
                        case diantou.dataModel.notice.type.UserThankAnswer:
                        case diantou.dataModel.notice.type.UserAnswerQuestion:
                        case diantou.dataModel.notice.type.UserInviteAnswered:
                        case diantou.dataModel.notice.type.UserCommentAnswer:
                        case diantou.dataModel.notice.type.AnswerAppendComment:
                        case diantou.dataModel.notice.type.UserAgreeAnswerComment:
                            if (Answer != null) return getPath(Answer.Path.Index, false);
                            break;
                        case diantou.dataModel.notice.type.UserFocus:
                            return getPath(User.Path.Index, false);
                        case diantou.dataModel.notice.type.UserFavoriteAnswer:
                        case diantou.dataModel.notice.type.UserFocusFavorite:
                            if (Favorite != null) return getPath(Favorite.Path.Index, false);
                            break;
                        case diantou.dataModel.notice.type.FavoriteAppendComment:
                        case diantou.dataModel.notice.type.UserAgreeFavoriteComment:
                            if (Favorite != null) return getPath(Favorite.Path.Index, true);
                            break;
                        case diantou.dataModel.notice.type.UserReworkQuestionTitle:
                        case diantou.dataModel.notice.type.UserReworkQuestionDescription:
                        case diantou.dataModel.notice.type.UserReworkQuestionLink:
                        case diantou.dataModel.notice.type.UserInviteAnswerQuestion:
                            return getPath(Question.Path.Index, false);
                        case diantou.dataModel.notice.type.QuestionAppendComment:
                        case diantou.dataModel.notice.type.UserAgreeQuestionComment:
                            return getPath(Question.Path.Index, true);
                        case diantou.dataModel.notice.type.UserReworkTopicName:
                        case diantou.dataModel.notice.type.UserReworkTopicDescription:
                        case diantou.dataModel.notice.type.UserReworkTopicImage:
                            return getPath(Topic.Path.Index, false);
                        case diantou.dataModel.notice.type.InviteUserUpGrade:
                            return getPath(User.Path.Notice, false);
                        case diantou.dataModel.notice.type.GetMedal:
                            return getPath(User.Path.OnlineJudgeMedal, false);
                        case diantou.dataModel.notice.type.AcceptedProblem:
                        case diantou.dataModel.notice.type.ReJudgeCancelAccepted:
                        case diantou.dataModel.notice.type.ContestProblem:
                            return getPath(Problem.IndexPath, false);
                        case diantou.dataModel.notice.type.ProblemTestUser:
                            return getPath(Problem.Path.Test, false);
                        case diantou.dataModel.notice.type.UserAgreeJudge:
                        case diantou.dataModel.notice.type.JudgeAppendComment:
                        case diantou.dataModel.notice.type.UserAgreeJudgeComment:
                        case diantou.dataModel.notice.type.UserCommentJudge:
                            return Notice.ContestId == 0 ? getPath(Notice.JudgePath, false) : getPath(Notice.ContestJudgePath, false);
                        case diantou.dataModel.notice.type.PassTutorial:
                            return getPath(Tutorial.Path.Index, false);
                        case diantou.dataModel.notice.type.UserUpGrade:
                            break;
                    }
                    return null;
                }
            }
            /// <summary>
            /// 格式化通知URL
            /// </summary>
            /// <param name="value"></param>
            /// <param name="isAutoComment">是否展开评论</param>
            /// <returns></returns>
            private string getPath(webView.hashUrl value, bool isAutoComment)
            {
                return value.Query != null ? value.Path + "#" + value.Query + "&noticeId=" + Notice.Id.toString() + (isAutoComment ? "&isAutoComment=1" : null) : getPath(value.Path, isAutoComment);
            }
            /// <summary>
            /// 格式化通知URL
            /// </summary>
            /// <param name="value"></param>
            /// <param name="isAutoComment">是否展开评论</param>
            /// <returns></returns>
            private string getPath(string value, bool isAutoComment)
            {
                return value + "#noticeId=" + Notice.Id.toString() + (isAutoComment ? "&isAutoComment=1" : null);
            }
        }
        /// <summary>
        /// 通知类型
        /// </summary>
        internal enum type
        {
            /// <summary>
            /// 未读
            /// </summary>
            None,
            /// <summary>
            /// 已读
            /// </summary>
            Readed,
            /// <summary>
            /// 系统
            /// </summary>
            System
        }
        /// <summary>
        /// 通知集合
        /// </summary>
        private diantou.dataAccess.notice[] notices;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private bool loadView(int page, type type)
        {
            switch (type)
            {
                case notice.type.Readed:
                    notices = diantou.dataAccess.tcpCall.notice.getOld(currentUser.Id, pageInfo.Size = fastCSharp.config.pub.Default.PageSize, pageInfo.Page = page, out pageInfo.Count);
                    break;
                case notice.type.System:
                    notices = diantou.dataAccess.tcpCall.notice.getSys(currentUser.Id, currentUser.IsChangeVisit, pageInfo.Size = fastCSharp.config.pub.Default.PageSize, pageInfo.Page = page, out pageInfo.Count);
                    break;
                default:
                    notices = diantou.dataAccess.tcpCall.notice.getNew(currentUser.Id, pageInfo.Size = fastCSharp.config.pub.Default.PageSize, pageInfo.Page = page, currentUser.IsChangeVisit, out pageInfo.Count);
                    break;
            }
            return true;
        }
    }
}
